package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AdminUser;
import com.smfx.yxt.common.Constants;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.WorkApprovalType;
import com.smfx.yxt.model.ComboBox;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.WorkApprovalConf;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.BasePageQueryVO;
import com.smfx.yxt.reqVo.TeacherReqVO;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.service.IWorkApprovalConfService;
import com.smfx.yxt.utils.StringUtil;

@RequestMapping("sys/workApproval/conf")
@Controller
public class WorkApprovalConfController {
	
	private Logger logger = LoggerFactory.getLogger(WorkApprovalConfController.class);

	@Autowired
	private IWorkApprovalConfService workApprovalConfService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping(value = {"","/"})
	public String index() {
		return "sys/workApprovalConf";
	}
	@RequestMapping("addAndUpdate")
	@ResponseBody Object addAndUpdate(HttpServletRequest request, WorkApprovalConf conf) {
		try {
			AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
			conf.setSchoolId(user.getTeacherInfo().getSchoolId());
			conf.setSchoolName(user.getTeacherInfo().getSchoolName());
			if (StringUtils.isNotBlank(conf.getApprovalerId())) {
				List<TeacherInfo> list = teacherService.selectListByIds(Arrays.asList(conf.getApprovalerId().split(",")));
				String approvaler = "";
				String approvalerId = "";
				for(int i = 0; i < list.size(); i++) {
					TeacherInfo teacher = list.get(i);
					approvaler += teacher.getName();
					approvalerId += teacher.getId();
					if((i+1) < list.size()){
						approvaler += ",";
						approvalerId += ",";
					}
				}
				conf.setApprovaler(approvaler);
				conf.setApprovalerId(approvalerId);
			}else {
				conf.setApprovaler("");
			}
			if(StringUtils.isNotBlank(conf.getCcerId())) {
				if(conf.getCcerId().equals("0")) {
					conf.setCcer("");
				}else {
					List<String> list = Arrays.asList(conf.getCcerId().split(","));
					List<TeacherInfo> teacherList = teacherService.selectListByIds(list);
					String ccer = "";
					String ccerId = "";
					for(int i = 0; i < teacherList.size(); i++) {
						TeacherInfo teacher = teacherList.get(i);
						ccer += teacher.getName();
						ccerId += teacher.getId();
						if((i+1) < teacherList.size()){
							ccer += ",";
							ccerId += ",";
						}
					}
					conf.setCcer(ccer);
					conf.setCcerId(ccerId);
				}
			}else{
				if(null != conf.getCcerId()) conf.setCcer("");
			}
			if(StringUtils.isNotBlank(conf.getItemCode())) {
				conf.setItemName(WorkApprovalType.getDesc(conf.getItemCode()));
			}
			workApprovalConfService.addAndUpdate(conf);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--add error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(HttpServletRequest request, BasePageQueryVO vo) {
		try {
			AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
			Page<WorkApprovalConf> page = new Page<WorkApprovalConf>();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("ID"));
			page.setSorts(sorts);
			vo.setSchoolId(user.getTeacherInfo().getSchoolId());
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			Grid grid = new Grid();
			List<WorkApprovalConf> list = workApprovalConfService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("----organizeWorkgroupManageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("teacherList")
	@ResponseBody Object teacherList(HttpServletRequest request, TeacherReqVO vo) {
		AdminUser user = (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
		vo.setSchoolId(user.getTeacherInfo().getSchoolId());
		vo.setTeacherName(vo.getQ());
		List<TeacherInfo> list = new ArrayList<>();
		TeacherInfo teacher = new TeacherInfo();
		teacher.setId(0);
		teacher.setName("请选择");
		list.add(teacher);
		List<TeacherInfo> resultList = teacherService.selectByParam(vo);
		if(resultList != null && resultList.size() > 0) {
			list.addAll(resultList);
		}
		return list;
	}
	
	@RequestMapping("type")
	@ResponseBody Object type() {
		List<ComboBox> list = new ArrayList<>();
		for(WorkApprovalType status : WorkApprovalType.values()) {
			list.add(new ComboBox(status.getCode(), status.getDesc()));
		}
		return list;
	}
	
}
